package top.raxidin.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import top.raxidin.common.VO.Result;
import top.raxidin.common.config.PasswordEncoderConfig;
import top.raxidin.sys.domain.DTO.GetUserListPageDTO;
import top.raxidin.sys.entity.User;
import top.raxidin.sys.service.IUserService;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author raxidin
 * @since 2024-11-10
 */

@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {



    @Autowired
    private IUserService userService;



    @ApiOperation("登录接口")
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user){
        return userService.login(user);
    }





    @GetMapping("/info")
    public Result<?> getUserInfo(@RequestParam String token){
        return userService.getUserInfo(token);
    }




    @PostMapping("/logout")
    public Result<?> logout(@RequestHeader("X-Token")String token){
        return userService.logout(token);
    }



/*    @GetMapping("/list")
    public Result<?> getUserListPage(@RequestParam(value = "username", required = false) String username,
                                     @RequestParam(value = "phone", required = false) String phone,
                                     @RequestParam("pageNo") Long pageNo,
                                     @RequestParam("pageSize") Long pageSize) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
        wrapper.eq(username != null && username!="", User::getUsername, username);
        wrapper.eq(phone != null && phone!="", User::getPhone, phone);
        Page<User> page = new Page<>(pageNo, pageSize);
        userService.page(page, wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());

        return Result.success(data);
    }*/

    @GetMapping("/list")
    public Result<?> getUserListPage(@RequestParam(value = "username", required = false) String username,
                                     @RequestParam(value = "phone", required = false) String phone,
                                     @RequestParam("pageNo") Long pageNo,
                                     @RequestParam("pageSize") Long pageSize) {
        GetUserListPageDTO getUserListPageDTO = new GetUserListPageDTO(username, phone, pageNo, pageSize);
        return userService.getUserListPage(getUserListPageDTO);
    }





    /*@PostMapping
    public Result<?> addUser(@RequestBody User user){
        log.debug("新增用户接口，参数为{}",user);
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        userService.save(user);
        return Result.success("新增用户成功");
    }*/

    @PostMapping
    public Result<?> addUser(@RequestBody User user){
        log.debug("新增用户接口，参数为{}",user);
        return userService.getUserByUserName(user);
    }




    @PutMapping
    public Result<?> updateUser(@RequestBody User user){
        log.debug("修改用户接口，参数为{}",user);
        user.setPassword(null);
        userService.updateUser(user);
        return Result.success("修改用户成功");
    }



    @GetMapping("/{id}")
    public Result<?> getUserById(@PathVariable("id") Integer id){
        log.debug("根据用户名查询用户，参数为{}",id);
        User user = userService.getUserById(id);
        user.setPassword(null);
        return Result.success(user);
    }


    @DeleteMapping("/{id}")
    public Result<?> deleteUserById(@PathVariable("id") Integer id){
        log.debug("删除用户，参数为{}",id);
        userService.deleteUserById(id);
        return Result.success("删除用户成功");
    }





}
